package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜品管理 前端控制器
 *
 * @author alkaid
 * @since 2024-02-19
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("")
    @ApiOperation(value = "新增菜品")
    public Result<Dish> create(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}", dishDTO);

        Dish dish = dishService.create(dishDTO);

        // 清理缓存数据
        // 构造 key
        cleanCache("dish" + dishDTO.getCategoryId());

        return Result.success(dish);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据 id 查询菜品")
    public Result<DishVO> queryById(@PathVariable("id") Long id) {
        log.info("根据 id 查询菜品：{}", id);

        DishVO dishVO = dishService.queryByIdWithFlavor(id);

        return Result.success(dishVO);

    }

    @GetMapping("/list")
    @ApiOperation("根据分类 id 查询菜品")
    public Result<List<Dish>> listByCategoryId(Long categoryId) {
        log.info("根据分类 id 查询菜品:{}", categoryId);

        List<Dish> dishList = dishService.listByCategoryId(categoryId);
        return Result.success(dishList);
    }

    @DeleteMapping
    @ApiOperation("根据 ids 批量删除菜品")
    public Result deleteByIds(@RequestParam List<Long> ids) {
        log.info("根据 ids 批量删除菜品:{}", ids);
        dishService.deleteByIds(ids);

        // 清除全部菜品缓存数据
        cleanCache("dish*");

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("起售、停售菜品")
    public Result<Dish> startOrStop(@PathVariable("status") Integer status, Long id) {

        log.info("修改菜品状态: {}, {}", id, status);

        Dish dish = dishService.startOrStop(id, status);

        // 清除全部菜品缓存数据
        cleanCache("dish*");

        return Result.success(dish);
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询菜品")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品:{}", dishPageQueryDTO);

        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result<DishVO> updateById(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}", dishDTO);
        DishVO dishVO = dishService.updateByIdWithFlavor(dishDTO);

        // 清除全部菜品缓存数据
        cleanCache("dish*");

        return Result.success(dishVO);
    }

    /**
     * 清理缓存数据
     *
     * @param pattern
     */
    private void cleanCache(String pattern) {
        redisTemplate.delete(
                redisTemplate.keys(pattern)
        );
    }


}
